package com.naiterui.ehp.bs.cms.modules.devops.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.modules.devops.service.VersionService;
import com.naiterui.ehp.bs.cms.modules.devops.vo.AppVersionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * @author luao
 * @date 2022/4/1 5:44 PM
 **/
@Api(tags = {SwaggerTag.CMS + SwaggerTag.DEVOPS})
@Slf4j
@Validated
@RestController
@RequestMapping("/version")
@RequiredArgsConstructor
@PreAuthorize("@p.check('devops:app:version')")
public class VersionController {

    private final VersionService versionService;

    @SysLog("app版本列表")
    @GetMapping
    @ApiOperation("app版本列表")
    // @PreAuthorize("@p.check('devops:app:version:list')")
    public List<AppVersionVO> list(Integer status) {
        return versionService.appVersionList(status);
    }

    @SysLog("新增app版本")
    @PostMapping
    @ApiOperation("新增app版本")
    // @PreAuthorize("@p.check('devops:app:version:save')")
    public void save(@Valid @RequestBody AppVersionVO appVersionVO) {
        log.info("新增app版本 params:{}", appVersionVO);
        versionService.addAppVersion(appVersionVO);
    }

    @SysLog("修改app版本")
    @PutMapping
    @ApiOperation("修改app版本")
    // @PreAuthorize("@p.check('devops:app:version:update')")
    public void update(@Valid @RequestBody AppVersionVO appVersionVO) throws BusinessException {
        log.info("修改app版本 params:{}", appVersionVO);
        versionService.updateAppVersion(appVersionVO);
    }

    @SysLog("版本启用")
    @PostMapping("/enable/{id}")
    @ApiOperation("版本启用")
    // @PreAuthorize("@p.check('devops:app:version:enable')")
    public void versionEnable(@PathVariable Long id) throws BusinessException {
        versionService.versionEnable(id);
    }

    @SysLog("版本禁用")
    @PostMapping("/disable/{id}")
    @ApiOperation("版本禁用")
    // @PreAuthorize("@p.check('devops:app:version:disabled')")
    public void versionDisabled(@PathVariable Long id) throws BusinessException {
        versionService.versionDisable(id);
    }

    @SysLog("上传APK")
    @PostMapping("/upload/apk")
    @ApiOperation("上传APK")
    // @PreAuthorize("@p.check('devops:app:version:upload')")
    public String uploadApk(MultipartFile file) throws BusinessException, IOException {
        return versionService.uploadApk(file);
    }
}
